package fm.douban;

import net.rim.device.api.system.WLANInfo;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.Font;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.ButtonField;
import net.rim.device.api.ui.component.Dialog;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.container.MainScreen;

public class WarningScreen extends MainScreen {
	private LabelField titleLabel = new LabelField(SongPlayScreen._resources
			.getString(RadioResource.APPLICATION_TITLE));

	private LabelField warning = new LabelField(SongPlayScreen._resources
			.getString(RadioResource.WARNING));

	private ButtonField wifi = new ButtonField(/*"Wifi Only"*/SongPlayScreen._resources
			.getString(RadioResource.WIFI_ONLY),ButtonField.CONSUME_CLICK);
	private ButtonField other = new ButtonField(/*"I don't have wifi."*/SongPlayScreen._resources
			.getString(RadioResource.NOT_WIFI),ButtonField.CONSUME_CLICK);
	private LabelField wifiNotEnabled = new LabelField(
			SongPlayScreen._resources.getString(RadioResource.WIFI_NOT_ENABLED));

	private FieldChangeListener listener = new FieldChangeListener() {
		public void fieldChanged(Field field, int context) {
			Radio.connectType = ";interface=wifi";
			if (!wifiCheck()) {
				Dialog.alert(SongPlayScreen._resources
						.getString(RadioResource.WIFI_NOT_ENABLED));
				return;
			}
			navigate();
		}
	};

	private FieldChangeListener listener1 = new FieldChangeListener(){
		public void fieldChanged(Field field, int context) {
			Radio.connectType = ";deviceside=true";
			navigate();
		};
	};

	public WarningScreen() {
		titleLabel.setFont(titleLabel.getFont().derive(Font.PLAIN,
				titleLabel.getFont().getHeight() + 2));
		super.setTitle(titleLabel);
		super.add(warning);
		if (!wifiCheck()) {
			super.add(wifiNotEnabled);
		}
		wifi.setChangeListener(listener);
		other.setChangeListener(listener1);
		super.add(wifi);
		super.add(other);

	}

	private static boolean wifiCheck() {
		if (WLANInfo.getWLANState() == WLANInfo.WLAN_STATE_CONNECTED) {
			return true;
		}
		return false;
	}

	protected boolean navigationClick(int status, int time) {
		return false;
	}

	private void navigate() {
		/*LoginUtil.getSavedAccount();
		Radio theApp = (Radio) UiApplication.getUiApplication();
		theApp.pushScreen(new SongPlayScreen());*/
		Radio theApp = (Radio) UiApplication.getUiApplication();
		if (!LoginUtil.checkDB())
			theApp.pushScreen(new LoginDialog());
		else {
			LoginUtil.getSavedAccount();
			if (LoginUtil.EMAIL.equals("1")
					|| LoginUtil.PASSWORD.equals("1"))
				theApp.pushScreen(new LoginDialog());
			else {
				LoginUtil
				.login(LoginUtil.EMAIL, LoginUtil.PASSWORD);
				theApp.pushScreen(new SongPlayScreen());
				// pushScreen(new ChannelScreen());
			}
		}
	}
}
